package com.nartosoft.tubuloader;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author eb, mf
 * 
 * Provides easy access to the configuration of PicUplader.
 */

public class FileConfiguration implements Configuration {

	public static final String PROXY_COMMAND = "proxy";

	public static final String LOGFILE_COMMAND = "logFile";

	public static final String LOGFILEFORMAT_COMMAND = "logParameters";

	public static final String TEMPLATE_COMMAND = "templateName";

	public static final String VERBOSE_SWITCH = "verbose";

	public static final String HTMLFILE_COMMAND = "historyFileName";

	public static final String SUMMARY_COMMAND = "summary";

	public static final String SOUND_COMMAND = "soundFileName";

	public static final String AUTOSCALE_COMMAND = "autoscale";
 
	public static final String TRAYICON_COMMAND = "trayicon";
	
	public static final String CLIENTID = "clientid";
	
	public static final String DEVELOPERID = "developerid";

	public static final String PASSWORD = "password";
	
	public static final String UPLOADURL = "uploadurl";

	public static final String USER = "user";

	public static final String MAXFILESIZE = "maxfilesize";

	private static final String CONFIGURATIONPATH = "./configurations/";
	
	 
	private Properties props=null;
	
	private String[] args = null;


	public FileConfiguration(String[] args) {
		try {
		  this.props = new Properties();
		  this.args = (String[])args.clone();
		  File f = new File(CONFIGURATIONPATH + args[0]);
		  FileInputStream fs = new FileInputStream(f);
		  this.props.load(fs);
		}catch(Exception e) {
			e.printStackTrace();
		}
	}


	public boolean isProxySet() {
		return props.containsKey(PROXY_COMMAND);
	}

	public boolean isLogFileSet() {
		return props.containsKey(LOGFILE_COMMAND);
	}

	public boolean isLogFileFormatSet() {
		return props.containsKey(LOGFILEFORMAT_COMMAND);
	}

	public boolean isHtmlFileSet() {
		return props.containsKey(HTMLFILE_COMMAND);
	}

	public boolean isTemplateSet() {
		return props.containsKey(TEMPLATE_COMMAND);
	}

	public boolean isSummarySet() {
		return props.containsKey(SUMMARY_COMMAND);
	}

	public boolean isSoundSet() {
		return props.containsKey(SOUND_COMMAND);
	}

	public boolean isAutoscaleSet() {
		return props.containsKey(AUTOSCALE_COMMAND);
	}

	public boolean isTrayiconSet() {
		return props.containsKey(TRAYICON_COMMAND);
	}
	
	public int getMaxFileSize() {
		return Integer.parseInt((String) props.get(MAXFILESIZE));
	}
	
	public String getSoundFile() {
		return (String) props.get(SOUND_COMMAND);
	}

	public boolean isVerboseSet() {
		return props.containsKey(VERBOSE_SWITCH);
	}

	public String getProxy() {
		return (String) props.get(PROXY_COMMAND);
	}

	public String getLogFileName() {
		return (String) props.get(LOGFILE_COMMAND);
	}

	public String getSummaryTime() {
		return (String) props.get(SUMMARY_COMMAND);
	}

	public String getTrayiconFile() {
		return (String)props.get(TRAYICON_COMMAND);
	}
	
	private String getLogFileFormat() {
		if (isLogFileFormatSet()) {
			return ((String) props.get(LOGFILEFORMAT_COMMAND)).toLowerCase();
		}
		else {
			return "";
		}
	}

	public boolean isImageFileLoggingSet() {
		return getLogFileFormat().indexOf('i') >= 0;
	}

	public boolean isUrlLoggingSet() {
		return getLogFileFormat().indexOf('u') >= 0;
	}

	public boolean isErrorLoggingSet() {
		return getLogFileFormat().indexOf('e') >= 0;
	}

	public String getTemplateFileName() {
		return (String) props.get(TEMPLATE_COMMAND);
	}

	public String getHtmlFileName() {
		return (String) props.get(HTMLFILE_COMMAND);
	}
	
    
	/**
	 * Returns the template string read from the template file, or null if either
	 * no template file was set or an error occured while reading it.
	 */
	public String getTemplate() {
		if (!isTemplateSet()) {
			return null;
		}
		return Utils.readFile(getTemplateFileName());
	}


	public List<String> getFileNames() {
		List<String> list = new ArrayList<String>();
		for (int i = 1; i < args.length; i++) {
		  list.add(args[i]);
		}
		return list;

	}
	
	public String getClientID() {
		return (String) props.get(CLIENTID);
	}

	public String getDeveloperID() {
		return (String) props.get(DEVELOPERID);
	}

	public String getPassword() {
		return (String) props.get(PASSWORD);
	}

	public String getUploadURL() {
		return (String) props.get(UPLOADURL);
	}


	public String getUser() {
		return (String) props.get(USER);
	}

}
